package com;

import com.client.Slave;
import com.util.ByteUtil;
import com.util.ListUtil;
import gnu.io.CommPortIdentifier;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

/**
 * Hello world!
 *
 */
public class App
{
    public static void main( String[] args )
    {
        try {
            Enumeration portList = CommPortIdentifier.getPortIdentifiers();
            while (portList.hasMoreElements()) {
                CommPortIdentifier portId = (CommPortIdentifier) portList.nextElement();
                System.out.println("获取到的串口名称为：COM" + portId.getPortType());
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

        //初始化150个寄存器空间，全部存0000，该空间存储16进制字符串
        List<String> list =new ArrayList();
        for (int i = 0; i < 150; i++) {
            list.add("0000");
        }
        //设置串口名，波特率，和寄存器空间
        Slave salve =new Slave(" /dev/ttyS",9600,list);
        salve.run();

        //Util.fillFloat(list,ByteUtil.hexStringToNum("00"),16*4,"0000");
        ListUtil.fillBollean(list,ByteUtil.hexStringToNum("40"),16*3,0);
        //Util.fillFloat(list,ByteUtil.hexStringToNum("70"),16,"0000");

        /*
        //设值
        ListUtil.setFloat(list,"10",38.9f);
        ListUtil.setFloat(list,"20",39f);
        ListUtil.setFloat(list,"30",40f);
        ListUtil.setBoolean(list,"50",0);
        ListUtil.setBoolean(list,"60",0);
        ListUtil.setBoolean(list,"70",0);*/

        //salve.inputCmd();
        //主动向传感器发送请求命令,此该当作master端在用
        while (true){
            try {
                salve.request();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
